<template>
  <a-modal
    title="修改手机号"
    width="45%"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
    destroyOnClose
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          :label="isNewPhoneModal ? '新手机号' : '原手机号'"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-input
            placeholder="请输入新的手机号码"
            :disabled="!isNewPhoneModal"
            v-decorator="['phone', {
              initialValue: isNewPhoneModal ? '' : info.phone,
              rules: [{required: true, pattern: regs.PHONE_REG, len: 11, message: '请输入正确的手机号码格式'}]}]"
          />
        </a-form-item>
        <a-form-item
          label="验证码"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-input-search
            v-decorator="['smsVerifyCode', {
              rules: [{required: true, pattern: /^\d{6}$/, len: 6, message: '请输入正确的验证码格式'}]}]"
            @search="handleGetVerifyCode"
          >
            <a-button
              slot="enterButton"
              :disabled="time !== 90"
              :type="verifyCodeAddonAfterType"
            >{{ verifyCodeAddonAfterText }}</a-button>
          </a-input-search>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { mapActions } from 'vuex'
import { sendVerifyCode, sendVerifyCodeWithAuth, checkVerifyCode } from '@/api/account'
import { updatePhone } from '@/api/login'
import regs from '@/constants/pattern'
export default {
  data() {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 }
      },
      form: this.$form.createForm(this),
      visible: false,
      confirmLoading: false,
      timer: null,
      time: 90,
      isNewPhoneModal: true
    }
  },
  props: {
    info: {
      type: Object,
      default: () => ({
        phone: ''
      })
    }
  },
  computed: {
    verifyCodeAddonAfterType() {
      if (this.time === 90) {
        return 'primary'
      }
      return 'default'
    },
    verifyCodeAddonAfterText() {
      if (this.time === 90) return '获取'
      return `${this.time}s 后重新获取`
    }
  },
  methods: {
    ...mapActions(['GetInfo']),
    handleGetVerifyCode() {
      if (this.isNewPhoneModal) {
        this.form.validateFields(['phone'], (errors, values) => {
          if (!errors) {
            sendVerifyCode({ phone: values.phone, graphVerifyCode: 1111 }).then(this.onSendVerifyCodeSuccess)
          }
        })
      } else {
        sendVerifyCodeWithAuth({ graphVerifyCode: 1111 }).then(this.onSendVerifyCodeSuccess)
      }
    },
    onSendVerifyCodeSuccess() {
      this.time = 90
      this.timer = setInterval(() => {
        if (this.time > 0) {
          this.time = this.time - 1
        } else {
          this.time = 90
          clearInterval(this.timer)
        }
      }, 1000)
    },
    handleSubmit() {
      this.confirmLoading = true
      this.form.validateFields((errors, values) => {
        if (!errors) {
          const { smsVerifyCode } = values
          if (this.isNewPhoneModal) {
            this.onCheckVerifyCode(values, () => {
              updatePhone({ smsVerifyCode, newPhone: values.phone })
                .then(() => {
                  this.GetInfo()
                  this.handleCancel()
                  this.$message.success('修改手机号码成功')
                })
                .finally(() => {
                  this.confirmLoading = false
                })
            })
          } else {
            this.onCheckVerifyCode(values, () => {
              this.isNewPhoneModal = true
              this.confirmLoading = false
              this.time = 90
              clearInterval(this.timer)
              this.form.setFieldsValue({
                phone: '',
                smsVerifyCode: ''
              })
            })
          }
        } else {
          this.confirmLoading = false
        }
      })
    },
    show() {
      this.visible = true
      if (this.info.phone) {
        this.isNewPhoneModal = false
      }
    },
    handleCancel() {
      this.visible = false
    },
    onCheckVerifyCode(params, onSuccess = () => { }) {
      checkVerifyCode(params).then(onSuccess).catch(() => {
        this.confirmLoading = false
        this.time = 90
        clearInterval(this.timer)
      })
    }
  }
}
</script>
